<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class XiangqingController extends Controller
{
	
	// 显示详情s
	public function getShow($id){
		// return 2222;
		// dd($id);
    	$yl=DB::table('frilk')->get();
    	$peizhi = DB::table('webcon')->first();
     	session(['peizhi'=>$peizhi]);
		//显现
		$cates=DB::table('goods')->where('id',$id)->first();
		// dd($cates);
		$cates->pic=ltrim($cates->pic,'.');
		$cateid=$cates->cateid;
		// 短评共几条
		$num=DB::table('comment')->where('gid',$id)->count();
		// 显示最新评论
		$comments=DB::table('comment')->join('user','comment.uid','=','user.id')->select('comment.*','user.username')->orderBy('comment.id','desc')->where('comment.gid',$id)->take(5)->get();
		$res=DB::table('goods')->where('cateid',$cateid)->take(3)->get();
		// dd($comments,$res);
		// 想读，在读的，个数
		$wish=DB::table('comment')->where('gid',$id)->where('interest','=','wish')->count();
		$do=DB::table('comment')->where('gid',$id)->where('interest','=','do')->count();
		$collect=DB::table('comment')->where('gid',$id)->where('interest','=','collect')->count();
		$do_page=[];
		$do_page['do']=$do;
		$do_page['collect']=$collect;
		$do_page['wish']=$wish;
		// 谁在都这本书；
		$who_do=DB::table('comment')->distinct('uid')->select('comment.date','comment.uid','user.pic','user.username')->where('comment.gid',$id)->where('comment.interest','=','do')->join('user','user.id','=','comment.uid')->take(4)->get();
		// dd($who_do);

		return view('/home/xiangqing/xiangqing',['cates'=>$cates,'res'=>$res,'comments'=>$comments,'id'=>$id,'yy'=>$yl,'num'=>$num,'do_page'=>$do_page,'who_do'=>$who_do]);
	}

	public function getBooks()
	{
		$uid=session('id');

		// dd($uid);
		$yl=DB::table('frilk')->get();

		if($uid){
		// 显示日记信息
		$res=DB::table('day')->where('uid',$uid)->orderBy('id','desc')->take(2)->get();
		$users=DB::table('user')->where('id',$uid)->get();
		$zwjs=DB::table('zwjs')->where('uid',$uid)->first();
		// dd($users);
		// 我在读的书
		$books=DB::table('comment')->join('goods','comment.gid','=','goods.id')->where('comment.uid',$uid)->where('comment.interest','do')->select('comment.*','goods.name','goods.pic')->get();
		// 显示最新3的照片
		$newp=DB::table('photo')->where('uid',$uid)->orderBy('id')->take(3)->get();
		// dd($newp,$uid);
		return view('/home/xiangqing/books',['res'=>$res,'users'=>$users,'yy'=>$yl,'zwjs'=>$zwjs,'books'=>$books,'newp'=>$newp]);
		}else{
			return view('home.login.login');
		}

	}

	//动态页面
	public function getDongtai(){
			// echo 2222;
			return view('/home/xiangqing/dongtai');
	}

	//评论点赞 
	public function getLove(Request $request){
		// echo 222;
		
		$num=$request->input('love');
		$uid=$request->input('uid');
		$cid=$request->input('cid');
		// dd($request->all());
		$love=$num + 1;
		$xx=$request->only('uid','cid');
		// dd($love,$uid,$cid);
		// $uid=2;
		if($uid){
			// 查询数据库有没有该用户点赞该条信息
			$cates=DB::table('love')->where(['cid'=>$cid,'uid'=>$uid])->first();
			// dd($cates);
			if(!$cates){
	    		// 插入数据
	    		$res1=DB::table('love')->insert($xx);
	    		// 更新数据库
				$res=DB::table('comment')->where(['id'=>$cid,'uid'=>$uid])->update(['love'=>$love]);
				// dd($res1,$res);
				if($res && $res1){
		    		echo 1;
		    	}else{
		    		echo 4;
		    	}
	    	}else{
	    		
		    	echo 3;
	    	}
	    }else{
	    	echo 2;
	    }	
	}

	// 自我介绍.ajax 无刷新
	public function getZwjs(Request $request){
		// return  333;
		// dd($request->all());
		$cates=$request->all();
		$uid=$request->input('uid');
		$content=$request->input('content');
		// 判断数据库里面是否有个人介绍
		$res1=DB::table('zwjs')->where('uid',$uid)->first();
		// dd($res1);
		if($res1){
			$res=DB::table('zwjs')->where('uid',$uid)->update(['content'=>$content]);
			// 更新成功
			echo 1;
		}else{
			// 写入zwjs表
			$res=DB::table('zwjs')->insert($cates);
			if($res){
				echo 2;  //插入成功
			}else{
				echo 3;  //插入
			}
			
		}
		
		// dd($res);

	}
	// ajax 无刷新更改评论信息
	public function getRmpl(Request $request){
		// dd($request->all());
		$id=$request->input('id');
		// dd($id);
		$cates=DB::table('comment')->where('gid',$id)->orderBy('love','desc')->take(5)->get();
		// dd($cates);
		if($cates){
			return $cates;
		}else{
			return 2;
		}

	}



}
